//
//  LRStatusOriginalV.swift
//  WeiboX
//
//  Created by admin on 05/12/2016.
//  Copyright © 2016 iOS_Objective-C. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class LRStatusOriginalV: UIView {
	
	var selfBottomConstraint: Constraint?
	
	var statusVM: LRStatusVM? {
		didSet {
			if let iconUrl = statusVM?.status?.user?.profile_image_url {
				icon.sd_setImage(with: URL.init(string: iconUrl), placeholderImage: UIImage.init(named: "avatar_default_big"))
			}
			
			name.text = statusVM?.status?.user?.screen_name
			mbRank.image = (statusVM?.mbRank)
			text.text = statusVM?.status?.text
			postTime.text = statusVM?.timeContent
			source.text = statusVM?.sourceContent
			veriyiedType.image = statusVM?.verifiedTypeImg
			
			selfBottomConstraint?.deactivate()
//			print(source.text)
			
			if let picUrl = statusVM?.status?.pic_urls, picUrl.count > 0 {
				// 如果有图
				print(picUrl.count)
				statusPic.isHidden = false
				
				self.snp.makeConstraints({ (make) in
					selfBottomConstraint = make.bottom.equalTo(statusPic).offset(StatusItemMargin).constraint
				})
				
				statusPic.picUrls = picUrl
			} else {
				statusPic.isHidden = true
				
				self.snp.makeConstraints({ (make) in
					selfBottomConstraint = make.bottom.equalTo(text).offset(StatusItemMargin).constraint
				})
			}
		}
	}
	
	private lazy var statusPic: LRStatusPicCollectionV = LRStatusPicCollectionV()
	
	private lazy var icon: UIImageView = UIImageView.init(image: UIImage.init(named: "avatar_default_big"))
	
	private lazy var veriyiedType: UIImageView = UIImageView.init(image: UIImage.init(named: "avatar_vip"))
	
	private lazy var mbRank: UIImageView = UIImageView.init(image: UIImage.init(named: "common_icon_membership_expired"))
	
	private lazy var name: UILabel = {
		let label = UILabel()
		label.text = "shino1r"
		label.font = UIFont.systemFont(ofSize: 14)
		return label
	}()
	
	private lazy var postTime: UILabel = {
		let label = UILabel()
		label.text = "刚刚"
		label.textColor = UIColor.orange
		label.font = UIFont.systemFont(ofSize: 12)
		return label
	}()
	
	private lazy var source: UILabel = {
		let label = UILabel()
		label.text = "来自: 星星的你"
		label.textColor = UIColor.lightGray
		label.font = UIFont.systemFont(ofSize: 11)
		return label
	}()
	
	private lazy var text: UILabel = {
		let label = UILabel()
		label.text = "测试文本 test text 测试文本 test text 测试文本 test text 测试文本 test text 测试文本 test text 测试文本 test text 测试文本 test text 测试文本 test text"
		label.font = UIFont.systemFont(ofSize: 14)
		label.numberOfLines = 0
		return label
	}()
	
	override init(frame: CGRect) {
		super.init(frame: frame)
		
		setupUI()
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}

	private func setupUI() {
		
		addSubview(statusPic)
		addSubview(icon)
		addSubview(veriyiedType)
		addSubview(name)
		addSubview(mbRank)
		addSubview(postTime)
		addSubview(source)
		addSubview(text)
		
		icon.snp.makeConstraints { (make) in
			make.leading.top.equalToSuperview().offset(StatusItemMargin)
			make.size.equalTo(CGSize(width: 44, height: 44))
		}
		
		veriyiedType.snp.makeConstraints { (make) in
			make.centerX.equalTo(icon.snp.trailing)
			make.centerY.equalTo(icon.snp.bottom)
		}
		
		name.snp.makeConstraints { (make) in
			make.leading.equalTo(icon.snp.trailing).offset(StatusItemMargin)
			make.top.equalTo(icon)
		}
		
		mbRank.snp.makeConstraints { (make) in
			make.leading.equalTo(name.snp.trailing).offset(StatusItemMargin)
			make.centerY.equalTo(name)
		}
		
		postTime.snp.makeConstraints { (make) in
			make.leading.equalTo(name)
			make.bottom.equalTo(icon)
		}
		
		source.snp.makeConstraints { (make) in
			make.leading.equalTo(postTime.snp.trailing).offset(StatusItemMargin)
			make.centerY.equalTo(postTime)
		}
		
		text.snp.makeConstraints { (make) in
			make.leading.equalToSuperview().offset(StatusItemMargin)
			make.trailing.equalToSuperview().offset(-StatusItemMargin)
			make.top.equalTo(icon.snp.bottom).offset(StatusItemMargin)
		}
		
		statusPic.snp.makeConstraints { (make) in
			make.leading.equalTo(text)
			make.top.equalTo(text.snp.bottom).offset(StatusItemMargin)
		}

		self.snp.makeConstraints { (make) in
			selfBottomConstraint = make.bottom.equalTo(statusPic).offset(StatusItemMargin).constraint
		}
	}
}


